// 各种按键事件的定义
// 点击书迁mark显示侧边栏menu

// 添加书签按钮
var category_button_add = document.getElementById('category_button_add')
category_button_add.onclick = function (){
    document.getElementById('category_button_add_text').style.display = 'none'
    document.getElementById('category_button_add_control').style.display = ''
}

var category_button_add_commit = document.getElementById('category_button_add_commit')
category_button_add_commit.onclick = function (){
    var input = document.getElementById('category_button_add_input').value;
    if(is_empty(input)){
        alert("请输入分类名")
    }else{
        api_add_category(input, function (data){
            if(data['code'] === 1){
                var category = document.createElement('div')
                category.innerHTML = read_category_html()
                set_category_node(category, data['category'])
                document.getElementById('menu_category').append(category)
            }else{
                alert(data['msg']);
            }
        })
    }
}
//鼠标移出按钮点击后取消添加
var is_in_category_button = false
document.onmousedown = function(event) {
    if(!is_in_category_button){
        document.getElementById('category_button_add_control').style.display = 'none'
        document.getElementById('category_button_add_text').style.display = ''
    }
}
category_button_add.onmouseover = function (){
    is_in_category_button = true
}
category_button_add.onmouseout  = function (){
    is_in_category_button = false
}


function read_category_html(){
    return document.getElementById('category_html_import').innerHTML
}
function read_tag_html(){
    return document.getElementById('tag_html_import').innerHTML
}

function set_category_node(node, data){
    if(node.childNodes.length > 0){
        node.childNodes.forEach(function (n){ set_category_node(n, data) })
    }
    if(node.className === 'category_button'){
        node.className = 'category_button';
        node.value = data;
        node.innerText = data['category'];
        node.onclick = function (){
            var category_buttons = document.getElementsByClassName("category_button");
            var category_name = document.getElementById("category_name");
            var category_del = document.getElementById('category_del')
            //修改按钮背景色
            for(var i=0;i<category_buttons.length;i++){
                category_buttons[i].style.background = '#a8a8a8';
            }
            node.style.background = '#292929';
            //修改分类标题
            category_name.innerText = node.innerText;
            //分类删除按钮
            category_del.onclick = function (){
                api_del_category(data['category_id'], function (data){
                    if(data['code'] === 1){
                        node.parentElement.remove()
                        document.getElementById('category_content').style.visibility = 'hidden'
                    }else{
                        alert(data['msg'])
                    }
                })
            }
            show_category_tags(node.value);
        }
    }
}

function refresh_category(all_tags_data){
    //获取原来的菜单
    var menu_category = document.getElementById('menu_category')
    //新建菜单列表，然后加载数据，替换
    var new_menu_category = document.createElement('dl');
    new_menu_category.className = 'menu_category'
    new_menu_category.id = 'menu_category'
    //根据数据添加按钮
    var category_html = read_category_html()
    Array.prototype.forEach.call(all_tags_data,function(category_data) {
        var category = document.createElement('div')
        category.innerHTML = category_html
        set_category_node(category, category_data)
        new_menu_category.append(category)
    });
    //替换
    menu_category.replaceWith(new_menu_category);
}

function set_tag_node(node, data){
    if(node.childNodes.length > 0){
        node.childNodes.forEach(function (n){ set_tag_node(n, data) })
    }
    if(node.className === 'tag_img'){
        node.src = get_server_url() + data['icon'];
    }else if(node.className === 'tag_url'){
        node.href = data['url'];
        node.innerText = data['name'];
        node.target = '_blank';
    }else if(node.className === 'tag_del'){
        node.onclick = function (){
            api_del_tag(data['id'],function (data){
                if(data['code'] === 1){
                    node.parentElement.parentElement.remove()
                }else{
                    alert(data['msg'])
                }
            })
        }
    }
}

function show_category_tags(data){
    //默认隐藏，改为显示
    document.getElementById('category_content').style.visibility = 'visible'
    //替换旧的列表
    var category_tags = document.getElementById('category_tags');
    //创建新的列表
    var new_category_tags = document.createElement('dl')
    new_category_tags.id = 'category_tags'
    new_category_tags.className = 'category_tags'
    //加载数据
    var category_tags_data = data['tags'];
    var tag_html = read_tag_html()
    Array.prototype.forEach.call(category_tags_data,function(tag_data) {
        var tag = document.createElement('div')
        tag.innerHTML = tag_html
        set_tag_node(tag, tag_data)
        new_category_tags.append(tag)
    });
    //替换
    category_tags.replaceWith(new_category_tags);
}

function refresh_data(){
    api_get_all_tag(function (data){
        if(data['code'] === 1){
            refresh_category(data['tags'])
        }
    })
}

refresh_data()